package com.zmovie.front.controller;

import com.alibaba.fastjson.JSON;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.service.*;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.SessionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.front.controller
 * @since 2023-12-23 17:40
 */
@Controller
@RequestMapping("/pay")
@Api(tags = "支付模块")
public class PayController {
    @Autowired
    private SessionService sessionService;
    @Autowired
    private HallService hallService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @GetMapping
    @ApiOperation("支付页面")
    public String index(Model model, HttpSession session){
        //选择的座位
        List<int[]> seats = (List<int[]>) session.getAttribute(Constants.SESSION_USER_SELECTED_SEATS_KEY);
        //选择的场次
        int sessionId = (int) session.getAttribute(Constants.SESSION_USER_SELECTED_SESSION_ID_KEY);
        Session sessionSelected = sessionService.getById(sessionId);
        model.addAttribute(Constants.SEATS_KEY, seats);
        model.addAttribute(Constants.SESSION_KEY, convert(sessionSelected));
        return "pay";
    }
    @PostMapping
    @ApiOperation("支付")
    @Transactional
    @ResponseBody
    public Object pay(HttpSession session){
        //用户
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        //订单
        Integer sessionId = (Integer) session.getAttribute(Constants.SESSION_USER_SELECTED_SESSION_ID_KEY);
        List<int[]> seats = (List<int[]>) session.getAttribute(Constants.SESSION_USER_SELECTED_SEATS_KEY);
        Order order = new Order();
        order.setState(1);
        order.setUserId(user.getId());
        order.setSessionId(sessionId);
        order.setSeat(JSON.toJSONString(seats));
        order.setDate(new Date());
        orderService.save(order);
        //修改场次座位信息
        Session sessionSelected = sessionService.getById(sessionId);
        List<int[]> sessionSeats = JSON.parseArray(sessionSelected.getSeats(), int[].class);
        for(int[] seat : seats){
            int row = seat[0], col = seat[1];
            //表示已经出售
            sessionSeats.get(row)[col] = 2;
        }
        sessionSelected.setSeats(JSON.toJSONString(sessionSeats));
        sessionService.updateById(sessionSelected);
        return CommonReturnType.success("支付成功!");
    }


    private SessionVO convert(Session session){
        SessionVO sessionVO = new SessionVO();
        BeanUtils.copyProperties(session, sessionVO);
        //电影
        Movie movie = movieService.getById(session.getMovieId());
        sessionVO.setMovie(movie);
        //影院
        Cinema cinema = cinemaService.getById(session.getCinemaId());
        sessionVO.setCinema(cinema);
        //联系电话
        User admin = userService.getById(cinema.getUserId());
        sessionVO.setPhone(admin.getPhone());
        //影厅
        Hall hall = hallService.getById(session.getHallId());
        sessionVO.setHall(hall);
        return sessionVO;
    }
}
